package com.example.orderservice.controller;

import com.example.orderservice.entity.Order;
import com.example.orderservice.entity.Product;
import com.example.orderservice.service.OrderService;
import jakarta.annotation.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author lenovo
 * @Description
 * @Date2025/4/14 下午4:38
 */
@RestController
public class OrderController {

    @Resource
    private OrderService orderService;

    @Resource
    private RestTemplate restTemplate;

    @RequestMapping("/order/{id}")
    public ResponseEntity<Order> findProductById(@PathVariable Integer id) {
        Order order = orderService.getById(id);
        //通过网络调用商品的查询服务
//        ResponseEntity<Product> entity=restTemplate.getForEntity("http://127.0.0.1:8001/product/" +order.getProductId(),
//                Product.class);

        //使用服务名+注册中心调用服务
        ResponseEntity<Product> entity=
                restTemplate.getForEntity("http://product-service/product/" +order.getProductId(),
                Product.class);

        order.setProduct(entity.getBody());
        return ResponseEntity.ok(order);
    }
}
